<?php

require_once 'constants.php';
require_once 'functions.php';
require_once 'controller.class.php';

class Dispatcher {
	
	private static $_instance;
	
	public $defaultController = "yamb";
	public $defaultAction = "index";
	
	public function __construct() {
		session_start();
		ob_start();
	}
	
	public static function getInstance() {
		if(!isset(self::$_instance)) {
			self::$_instance = new Dispatcher();
		}
		return self::$_instance;
	}
	
	public static function dispatch() {
		$D = self::getInstance();
		
		$params = array_merge($_GET, $_POST);
		$dispatch = isset($params["dispatch"]) ? explode("/", $params["dispatch"]) : array();
		
		$controller = isset($dispatch[0]) ? $dispatch[0] : $D->defaultController;
		$action = isset($dispatch[1]) ? $dispatch[1] : $D->defaultAction;
		$attributes = count($dispatch)>2 ? array_slice($dispatch,2) : array();
		
		$class = name2class($controller);
		
		$object = new $class;
		object_call_method($object, $action, $attributes);
	}
}

?>